package com.example.demo.controller;

import com.example.demo.constant.Constants;
import com.example.demo.model.Result;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService service;

    /**
     * 用户注册
     * @param username 用户名
     * @param password 密码
     * @return Result
     */
    @PostMapping("/register")
    @ResponseBody
    public Result<String> register(String username, String password){
        log.info("接收到对象为 username {}",username);
        //1.校验客户端数据
        if (!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return Result.fail("输入的账号或密码为空");
        }
        //2.校验当前输入的账号是否存在

        if (service.querySelect(username)!=null){
            return Result.fail("当前用户名存在");
        }
        //3.将用户信息存储到数据库
        Integer i = service.queryInsert(username, password);
        return Result.success("注册成功"+i);
    }
    @PostMapping("/login")
    @ResponseBody
    public Result<User> login(String username, String password, HttpServletRequest request){
        log.info("接收到对象为 username {}",username);
        //1.校验客户端数据
        if (!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return Result.fail("输入的账号或密码为空");
        }
        //2.校验用户输入的账号密码是否正确

        User user = service.querySelect(username);
        if (user==null){
            return Result.fail("该用户不存在");
        }

        if (!user.getPassword().equals(password)){
            return Result.fail("密码错误,重新输入");
        }
        request.getSession().setAttribute(Constants.USER_SESSION_KEY,user);
        log.info("session {}",request.getSession());
        User safetyUser=new User();
        safetyUser.setUserId(user.getUserId());
        safetyUser.setTotalCount(user.getTotalCount());
        safetyUser.setWinCount(user.getWinCount());
        safetyUser.setWinCount(user.getWinCount());
        safetyUser.setUsername(user.getUsername());
        safetyUser.setScore(user.getScore());

        return Result.success(safetyUser);
    }
    @GetMapping("/userInfo")
    public User getUserInfo(HttpServletRequest request){
        User safetyUser=new User();
        try {
            HttpSession session = request.getSession();
            User attribute = (User) session.getAttribute(Constants.USER_SESSION_KEY);
            safetyUser.setScore(attribute.getScore());
            safetyUser.setUsername(attribute.getUsername());
            safetyUser.setTotalCount(attribute.getTotalCount());
            safetyUser.setWinCount(attribute.getWinCount());
            safetyUser.setUserId(attribute.getUserId());
        }catch (NullPointerException e){
            return null;
        }

        return safetyUser;
    }
}
